package com.atstudying.mall.order.web;

import com.atstudying.mall.order.service.OrderService;
import com.atstudying.mall.order.vo.OrderConfirmVo;
import com.atstudying.mall.order.vo.OrderSubmitVo;
import com.atstudying.mall.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @author Mr.zhneg
 * @create 2020-11-21-12:02
 */
@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;
    @GetMapping("/toTrade")
    public String toTrade(Model model){
        OrderConfirmVo confirmVo= orderService.confirmOrder();
        model.addAttribute("orderConfirmData",confirmVo);

        return "confirm";
    }

    /**
     * 提交订单功能
     * @param vo
     * @return
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo,Model model){
        //todo 下单：去创建订单，验证令牌，验证价格，锁库存。。。。。就是去submitOrder方法中执行

        SubmitOrderResponseVo submitOrderResponseVo=submitOrderResponseVo=orderService.submitOrder(vo);
        System.out.println("订单提交的数据。。"+vo);

        if (submitOrderResponseVo.getCode()==0){
            //下单成功来到支付选择页
            model.addAttribute("submitOrderResponseVo",submitOrderResponseVo);

            return "pay";
        }else {
            //下单失败回到订单确认页重新确认订单信息
            return "redirect:http://order.mall.com/toTrade";

        }
    }
}
